package util;

import java.util.UUID;

public class UUIDUtil {

    private static byte[] lock1 = new byte[0];
    private static byte[] lock2 = new byte[0];

    // 位数，默认是8位
    private final static long eight = 100000000;
    private final static long three = 1000;

    public static String createID() {
        long r = 0;
        synchronized (lock1) {
            r = (long) ((Math.random() + 1) * eight);
        }

        return System.currentTimeMillis() + String.valueOf(r).substring(1);
    }

    public static String createSixteenID() {
        long r = 0;
        synchronized (lock2) {
            r = (long) ((Math.random() + 1) * three);
        }

        return System.currentTimeMillis() + String.valueOf(r).substring(1);
    }

    /**
     * 返回一个UUID
     *
     * @return 小写的UUID
     */
    public static String uuid() {
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        return s.substring(0, 8) + s.substring(9, 13) +
                s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
    }

    /**
     * 返回一个UUID
     *
     * @return 大写的UUID
     */
    public static String UUID() {
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        String temp = s.substring(0, 8) + s.substring(9, 13) +
                s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
        return temp.toUpperCase();
    }

    /**
     * 返回一个UUID
     *
     * @return 大写的UUID
     */
    public static String getUUID() {
        UUID uuid = UUID.randomUUID();
        String s = uuid.toString();
        String temp = s.substring(0, 8) + s.substring(9, 13) +
                s.substring(14, 18) + s.substring(19, 23) + s.substring(24);
        return temp;
    }
}
